// pages/my/bank-card/index.js
import {
    _headerPost,
    _headerGet,
} from '../../../../utils/httpUtils.js'
const RSA = require("../../../../utils/wx_rsa.js");
const app = getApp();
Page({
    data: {
        edit: false,
        navbarData: {
            showCapsule: 1, //是否显示左上角图标
            title: '我的银行卡' //导航栏 中间的标题
        },
        height: app.globalData.height * 2 + 20
    },
    onLoad(e) {
        if (e.Type != undefined)
            this.setData({
                Type: e.Type
            })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onShow() {
        this.getBankCard()
    },
    //获取银行卡信息
    getBankCard(page) {
        _headerGet('/api/UserBankCard/getList', {
            page: page || 1
        }, res => {
            if (res.data.code == 1) {
                this.setData({
                    bankList: res.data.data.list
                })
            }
        })
    },
    //选中银行卡
    seleteCard(e) {
        if (this.data.Type == 'selete') {
            let item = this.data.bankList[e.currentTarget.id]
            let data = {}
            data.ubc_id = item.ubc_id
            data.ubc_name = item.ubc_name
            data.ubc_num = item.ubc_num
            //更新上个选择界面内容
            let pages = getCurrentPages()
            let prevPage = pages[pages.length - 2]
            prevPage.setData({
                bankCard: data
            })
            wx.navigateBack({})
        }
    },
    //编辑银行卡
    editBtn() {
        this.setData({
            edit: !this.data.edit
        })
    },
    //添加银行卡
    addBankCard() {
        wx.navigateTo({
            url: '../add-bank/index',
        })
    },
    //解除绑定银行卡
    delBank(e) {
        wx.showModal({
            title: '提示',
            content: '您确定要解除绑定吗？',
            success: res => {
                if (res.confirm) {
                    _headerPost('/api/UserBankCard/bank_del', {
                        ubc_id: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${e.currentTarget.id}`)),
                        c_client: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`weixinSmall`))
                    }, res => {
                        if (res.data.code == 1) {
                            this.getBankCard()
                            wx.showToast({
                                title: '解绑成功'
                            })
                        } else {
                            wx.showToast({
                                title: res.data.msg,
                                icon: 'none'
                            })
                        }
                    })
                }
            }
        })
    },
    //设置默认银行卡
    setDefCard(e) {
        _headerPost('/api/UserBankCard/setDefault', {
            ubc_id: e.currentTarget.id
        }, res => {
            if (res.data.code == 1) {
                this.getBankCard()
                wx.showToast({
                    title: '设置成功',
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                })
            }
        })
    }
})